﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.SqlServer.Management.Trace;
using Microsoft.SqlServer.Management.Common;


namespace SqlWatcher
{
    class Program
    {

        
        
    //private static bool BackgroundThreadStop = false;
    //private static object QueueLock = new object();
    //private static List<object[]> EventQueue = new List<object[]>();
    //private static System.Threading.Thread backgroundThread = new System.Threading.Thread(BackgroundThread);
    //private static IPAddress _ipAddress = IPAddress.Parse(ConfigurationManager.AppSettings["SourceIP"].ToString());
    //private static Int32  _port = Int32.Parse(ConfigurationManager.AppSettings["SendPort"].ToString());

        
    static void Main(string[] args)
    {

        List<SqlReader> watchlist1 = new List<SqlReader>();   
        try
            {
                foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings )
                {
                    if (key.Name.StartsWith("sql"))
                    {
                        SqlReader sqlRead = new SqlReader();
                        sqlRead.Start(key.ToString());
                        Console.WriteLine(key.ToString() + " added");
                        watchlist1.Add(sqlRead);
                        Console.WriteLine(key.ToString() + " added to list");
                        
                    }
                }


                
            }
            catch (Exception ex)
            {
                //StreamWriter ew = new StreamWriter(ConfigurationManager.AppSettings["ErrorLogPath"].ToString(), true, Encoding.UTF8);
                Console.WriteLine(ex.Message + "\n\r" + ex.StackTrace);
                //ew = null;
            }
            finally
            {
                watchlist1 = null;
                
            }

    //    TraceServer reader = new TraceServer();
    //    bool isStopped = false;
    //    backgroundThread.Start();
    //    while (!isStopped)
    //    {
    //        try
    //        {
    //            string conn = "SCSPROI003,10.3.33.28,55902";
    //            string[] connParams = conn.Split(',');
    //            ConnectionInfoBase ci = new SqlConnectionInfo(connParams[0]);
    //            _ipAddress = IPAddress.Parse(connParams[1]);
    //            _port = Int32.Parse(connParams[2]);
    //            ((SqlConnectionInfo)ci).UseIntegratedSecurity = true;

    //            reader.InitializeAsReader(ci, @"IT_Sabotage.tdf");

    //            while (reader.Read())
    //            {
    //                // put line into array
    //                Object[] values = new Object[reader.FieldCount];
    //                for (int i = 0; i < reader.FieldCount; i++)
    //                {
    //                    values[i] = reader.GetValue(i);
    //                    //reader.GetValues(values);


    //                }

    //                // add array to queue
    //                lock (QueueLock)
    //                {
    //                    EventQueue.Add(values);
    //                }
    //                values = null;
    //            }
    //            reader.Close();
    //        }
    //        catch (Exception ex)
    //        {
    //            Console.WriteLine(ex.Message);

    //        }
    //    }   

    //}


    //private static void BackgroundThread()
    //{
    //    while (!BackgroundThreadStop)
    //    {
    //        bool shouldSleep = true;

    //        lock (QueueLock)
    //        {

    //            if (EventQueue.Count != 0)
    //            {
    //                shouldSleep = false;

    //            }
    //        }

    //        if (shouldSleep)
    //        {
    //            System.Threading.Thread.Sleep(10000);
    //        }
    //        else // should process the queue
    //        {
    //            List<object[]> ourQueue;
    //            lock (QueueLock)
    //            {
    //                // swap queues, giving the capture callback a new one
    //                ourQueue = EventQueue;
    //                EventQueue = new List<object[]>();
    //                ArrayList input = new ArrayList { ourQueue, _ipAddress, _port };
    //                //ThreadHelper.StartSocketSend((object)input);
    //                SocketSendHelper.SendEvent((object)input);
    //            }
    //        }
    //    }

    }

    }
}



